package com.dj.mingkong.admin.biz.repository.database.music;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.music.vo.request.MusicianQueryVo;
import com.dj.mingkong.admin.biz.util.WrapperUtil;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.eo.BaseEo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.music.MusicianEo;
import com.dj.mingkong.admin.biz.repository.database.music.mapper.MusicianMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Objects;


/**
 * 音乐人repository
 *
 * @author zenglonghui
 * @date: 2022/5/18 9:36
 */
@Repository
public class MusicianRepository extends BaseDatabaseRepository<MusicianMapper, MusicianEo, QueryVo> {
    @Override
    public Wrapper<MusicianEo> buildQuery(MusicianEo musicianEo) {
        if (musicianEo == null) {
            return null;
        }
        LambdaQueryWrapper<MusicianEo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(musicianEo.getId() != null, MusicianEo::getId, musicianEo.getId())
                .like(StringUtils.isNotBlank(musicianEo.getStageName()), MusicianEo::getStageName, musicianEo.getStageName())
                .like(StringUtils.isNotBlank(musicianEo.getRealName()), MusicianEo::getRealName, musicianEo.getRealName())
                .like(StringUtils.isNotBlank(musicianEo.getDescription()), MusicianEo::getDescription, musicianEo.getDescription())
                .like(StringUtils.isNotBlank(musicianEo.getCity()), MusicianEo::getCity, musicianEo.getCity())
                .eq(musicianEo.getSex() != null, MusicianEo::getSex, musicianEo.getSex())
                .ge(musicianEo.getAge() != null, MusicianEo::getAge, musicianEo.getAge())
                .eq(musicianEo.getUserId() != null, MusicianEo::getUserId, musicianEo.getUserId())
                .eq(musicianEo.getStatus() != null, MusicianEo::getStatus, musicianEo.getStatus())
                .orderByDesc(MusicianEo::getCreateTime);
        return queryWrapper;
    }

    public MusicianEo oneByUserIdAndStatus(Long userId, Integer status) {
        return lambdaQuery().eq(MusicianEo::getUserId, userId).eq(MusicianEo::getStatus, status).one();
    }

    public Page<MusicianEo> listPageByVo(MusicianQueryVo queryVo) {
        final QueryWrapper<MusicianEo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Objects.nonNull(queryVo.getId()), BaseEo::getId, queryVo.getId())
                .like(StringUtils.isNotBlank(queryVo.getStageName()),
                        MusicianEo::getStageName, queryVo.getStageName())
                .eq(Objects.nonNull(queryVo.getStatus()), MusicianEo::getStatus, queryVo.getStatus())
                .eq(StringUtils.isNotBlank(queryVo.getGenStatus()), MusicianEo::getGenStatus, queryVo.getGenStatus())
                .in(StringUtils.isNotBlank(queryVo.getSex()), MusicianEo::getSex,
                        Arrays.stream(queryVo.getSex().split(CommonConstants.SPLIT_COMMA)).toList());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }
}
